package nyist.media.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * excel 导入工具类
 *
 * @author ck
 * @date 2018-08-21
 */
@Slf4j
public final class ExcelUtil {

    /**
     * 读取excel文件（2007版）
     * @param in 文件输入流
     * @return 内容信息
     */
    public static List<ExcelRow> readExcel(InputStream in, int sheetIndex) {
        Workbook wb = null;
        try {
            // 初始化Workbook对象
            wb = new XSSFWorkbook(in);
            Sheet sheet = wb.getSheetAt(sheetIndex);
            return readExcel(sheet);
        } catch (Exception e) {
            log.error("导入数据失败");
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    log.error("流关闭失败");
                }
            }
        }
        return null;
    }

    private static List<ExcelRow> readExcel(Sheet sheet) {
        List<ExcelRow> data = new ArrayList<>();

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            int lastCellNum = row.getLastCellNum();

            List<Object> colList = new ArrayList<>();
            // 是否全部为空
            boolean allColsBlank = true;
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                Object value = "";
                if (cell == null) {
                    colList.add(value);
                    continue;
                }

                switch (cell.getCellTypeEnum()) {
                    case STRING:
                        value = cell.getStringCellValue();
                        break;
                    case NUMERIC:
                        value = String.valueOf(cell.getNumericCellValue());
                        break;
                    case BOOLEAN:
                        value = Boolean.valueOf(cell.getBooleanCellValue());
                        break;
                    case BLANK:
                        value = "";
                        break;
                    default:
                        value = cell.toString();
                }

                if (StringUtils.isNotEmpty(value.toString())) {
                    allColsBlank = false;
                }
                colList.add(value);
            }

            if (!allColsBlank) {
                ExcelRow excelRow = new ExcelRow();
                excelRow.setRow(i - 1);
                excelRow.setData(colList);
                data.add(excelRow);
            }
        }
        return data;
    }

    public static void main(String[] args) {
        try {
            ///
//            readExcel(new FileInputStream("E:\\work\\oye-parent\\oye-admin\\target\\classes\\static\\temp-upload\\oye\\oye_init.xlsx"), 0).forEach(t -> System.out.println(t));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
